package com.wmh.baseservice.skd.controller;

import com.wmh.baseservice.common.service.TokenService;
import com.wmh.baseservice.common.shiro.jwt.JwtTokenUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.user.pojo.vo.UsInfoRespVO;
import com.wmh.baseservice.user.service.UsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@Api(tags = "用户信息接口")
@Validated
@RestController
@RequestMapping(value = "users", produces = "text/plain;charset=utf-8")
@Slf4j
public class UserInfoController {
    @Resource
    private HttpServletRequest request;
    @Resource
    private UsInfoService usInfoService;
    @Resource
    private TokenService tokenService;

    /**
     * 根据token获取用户id
     * @return 返回用户id
     */
    private Long getUserIdFromToken() {
        String jwt = request.getHeader("Authorization");
        jwt = jwt.replace("Bearer_", "");
        if (Boolean.FALSE.equals(JwtTokenUtil.isTokenExpired(jwt))) {
            throw new IllegalStateException("令牌过期或无效!");
        }
        return Long.parseLong(JwtTokenUtil.getUserInfoFromToken(jwt).get("usId").toString());
    }

    /**
     * #0007 用户端编辑个人信息
     * @param nick 用户昵称
     * @param avatar 用户头像
     * @param phone 用户手机号码
     * @return 返回修改状态
     */
    @ApiOperation("用户端编辑个人信息")
    @PutMapping("info")
    public String editInfo(@RequestParam(value = "nick", required = false) String nick,
                           @RequestParam(value = "avatar", required = false) MultipartFile avatar,
                           @RequestParam(value = "phone", required = false) String phone) {
        try {
            Long usId = getUserIdFromToken();
            usInfoService.editUsInfo(nick, avatar, phone, usId);
            return ReturnBody.success("修改成功!");
        } catch (IllegalStateException e) {
            return ReturnBody.error(e.getMessage());
        } catch (Exception e) {
            return ReturnBody.error("修改失败!");
        }
    }

    /**
     * #0008 个人主页获取头像名称
     * @return 返回用户头像url和昵称
     */
    @ApiOperation("用户端获取个人信息")
    @GetMapping("info")
    public String getUsInfo() {
        try {
            Long usId = getUserIdFromToken();
            UsInfoRespVO usInfoRespVO = usInfoService.getAvatarAndNickByUsId(usId);
            return ReturnBody.success(usInfoRespVO);
        } catch (IllegalStateException e) {
            return ReturnBody.error(e.getMessage());
        } catch (Exception e) {
            return ReturnBody.error("获取失败!");
        }
    }

    /**
     * #0010 是否未设置头像昵称
     * @return 未设置返回false，已设置返回true
     */
    @ApiOperation("是否未设置头像昵称(新用户头像昵称是否均为空)")
    @GetMapping("is-new")
    public String isNewUser() {
        try {
            Long usId = getUserIdFromToken();
            Boolean res = usInfoService.isNewUser(usId);
            return ReturnBody.success(res);
        } catch (IllegalStateException e) {
            return ReturnBody.error(e.getMessage());
        } catch (Exception e) {
            return ReturnBody.error("获取失败!");
        }
    }

    /**
     * #0077 查看用户余额
     * @return 返回用户账户余额
     */
    @ApiOperation("查看用户余额")
    @GetMapping("balance")
    public String getBalance() {
        Long usId;
        try {
            // 根据token获取用户id
            usId = tokenService.getUserIdFromToken();
        } catch (IllegalStateException e) {
            log.error(e.getMessage());
            return ReturnBody.error("token异常!");
        }
        BigDecimal balance = usInfoService.getBalanceById(usId);
        return ReturnBody.success(balance);
    }


}
